home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / MLTE SDK / MLTE_Hands-on / MLTESampleShell / MLTESampleShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-15  |  18.1 KB  |  795 lines  |  [TEXT/CWIE]

  1. /* --------------------------------------------------------------
  2.     Apple Worldwide Developer Technical Support
  3.  
  4.     MLTESampleShell.c
  5.  
  6.     You may incorporate this sample code into your applications without
  7.     restriction, though the sample code has been provided "AS IS" and the
  8.     responsibility for its operation is 100% yours.  However, what you are
  9.     not permitted to do is to redistribute the source as "Apple Sample Code"
  10.     after having made changes. If you're going to re-distribute the source,
  11.     we require that you make it clear in the source that the code was
  12.     descended from Apple Sample Code, but that you've made changes.
  13.  
  14.     Copyright © 2000 Apple Computer, Inc., All Rights Reserved
  15.     
  16. ----------------------------------------------------------------- */
  17. #include "MLTESampleShell.h"
  18.  
  19. void main(void)
  20. {    
  21.     Initialize();
  22.     EventLoop();
  23. }
  24.  
  25. void Initialize()
  26. {
  27.     Handle        menuBar;
  28.     OSStatus     status = noErr;
  29.     
  30.     MaxApplZone();
  31.     InitGraf((Ptr) &qd.thePort);
  32.     InitFonts();
  33.     InitWindows();
  34.     InitMenus();
  35.     FlushEvents(everyEvent,0);
  36.     TEInit();
  37.     InitDialogs(NULL);
  38.     InitCursor();
  39.     InstallAppleEventHandlers();
  40.     CheckSystemVersion();    
  41.     
  42.     // Step 1 -- Initialize MLTE
  43.  
  44.     if (status != noErr)
  45.         BigBadError(eNoMLTE);
  46.  
  47.     menuBar = GetNewMBar(rMenuBar);        // read in menubar definition
  48.     if (menuBar == NULL)
  49.         BigBadError(eNoMemory);
  50.     SetMenuBar(menuBar);                // install menus
  51.     DisposeHandle(menuBar);
  52.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    // add Apple Menu Items
  53.  
  54.     // Step 27 -- Create the TXNFontMenuObject
  55.  
  56.     if (status != noErr)
  57.         BigBadError(eNoCreateFontMenuObject);
  58.  
  59.     DrawMenuBar();
  60.  
  61.     gInBackground = false;    
  62.     gNumDocuments = 0;
  63.     DoNew();    // create a single empty document
  64. }
  65.  
  66. // Check for presence of MLTE
  67. static void CheckSystemVersion(void) 
  68. {
  69.     if (TXNVersionInformation == (void*)kUnresolvedCFragSymbolAddress)
  70.         BigBadError(eWrongSystem);
  71. }
  72.  
  73. // Get events -- handle them in DoEvent. Call AdjustCursor each time through the loop.
  74. void EventLoop()
  75. {
  76.     RgnHandle    cursorRgn;
  77.     Boolean        gotEvent;
  78.     EventRecord    event;
  79.  
  80.     cursorRgn = NewRgn();    // pass WNE an empty region the 1st time thru
  81.  
  82.     do {
  83.         gotEvent = WaitNextEvent(everyEvent, &event, GetSleep(), cursorRgn);
  84.  
  85.         if (gotEvent)
  86.         {
  87.             // make sure we have the right cursor before handling the event
  88.             AdjustCursor(cursorRgn);
  89.             DoEvent(&event);
  90.         }
  91.         else
  92.         {
  93.             DoIdle();        // perform idle tasks when it’s not our event
  94.             // Step 7 -- give time to TSM.
  95.             
  96.         }
  97.     } while (true);            // loop forever... we quit via ExitToShell
  98. }
  99.  
  100. // Determine what kind of event we have, and call the appropriate routines.
  101. void DoEvent(EventRecord *event)
  102. {
  103.     short        part, err;
  104.     WindowPtr    window;
  105.     
  106.     switch (event->what) 
  107.     {
  108.         case nullEvent:
  109.             DoIdle();
  110.             break;
  111.         case mouseDown:
  112.             part = FindWindow(event->where, &window);
  113.             switch (part) 
  114.             {
  115.                 case inMenuBar:             // process a mouse menu command (if any)
  116.                     AdjustMenus();            // bring ’em up-to-date
  117.                     DoMenuCommand(MenuSelect(event->where));
  118.                     break;
  119.                 case inSysWindow:           // let the system handle the mouseDown
  120.                     SystemClick(event, window);
  121.                     break;
  122.                 case inContent:
  123.                     if (window != FrontWindow()) 
  124.                         SelectWindow(window);
  125.                     else
  126.                     {
  127.                         DoContentClick(window, event);
  128.                         AdjustMenus();
  129.                     }
  130.                     break;
  131.                 case inDrag:                // pass screenBits.bounds to get all gDevices
  132.                     DragWindow(window, event->where, &qd.screenBits.bounds);
  133.                     break;
  134.                 case inGoAway:
  135.                     if (TrackGoAway(window, event->where))
  136.                         DoCloseWindow(window); // we don’t care if the user cancelled
  137.                     break;
  138.                 case inGrow:
  139.                     DoGrowWindow(window, event);
  140.                     break;
  141.                 case inZoomIn:
  142.                 case inZoomOut:
  143.                     if (TrackBox(window, event->where, part))
  144.                         DoZoomWindow(window, part);
  145.                     break;
  146.             }
  147.             break;
  148.         case keyDown:
  149.         case autoKey:                       // check for menukey equivalents 
  150.             if (event->modifiers & cmdKey)  // Command key down
  151.             {
  152.                 if (event->what == keyDown) 
  153.                 {
  154.                     AdjustMenus();            // enable/disable/check menu items properly
  155.                     DoMenuCommand(MenuKey(event->message & charCodeMask));
  156.                 }
  157.             } 
  158.             else
  159.                 DoKeyDown(event);
  160.             break;
  161.         case activateEvt:
  162.             DoActivate((WindowPtr) event->message, (event->modifiers & activeFlag) != 0);
  163.             break;
  164.         case updateEvt:
  165.             DoUpdate((WindowPtr) event->message);
  166.             break;
  167.         case diskEvt:    // handle disk inserted events to recognize uninitialized floppies.
  168.             if (HiWord(event->message) != noErr) 
  169.             {
  170.                 Point    aPoint;
  171.                 SetPt(&aPoint, kDILeft, kDITop);
  172.                 err = DIBadMount(aPoint, event->message);
  173.             }
  174.             break;
  175.         case kOSEvent:
  176.             // BitAND with 0x0FF to get only low byte
  177.             switch ((event->message >> 24) & 0x0FF) // high byte of message
  178.             {        
  179.                 case mouseMovedMessage:
  180.                     DoIdle();                // mouse-moved is also an idle event 
  181.                     break;
  182.                 case suspendResumeMessage:    // suspend/resume is also an activate/deactivate 
  183.                     gInBackground = (event->message & kResumeMask) == 0;
  184.                     DoActivate(FrontWindow(), !gInBackground);
  185.                     break;
  186.             }
  187.             break;
  188.     }
  189. }
  190.  
  191. //    Change the cursor's shape, depending on its position.
  192. void AdjustCursor(RgnHandle region)
  193. {
  194.     WindowPtr    window = FrontWindow();    // only adjust when we are in front        
  195.     TXNObject     object = NULL;
  196.  
  197.     // Step 8 -- let MLTE adjust the cursor.
  198.  
  199. }
  200.  
  201. // Handle a mouseDown in the grow box of an active window.
  202. void DoGrowWindow(WindowPtr window, EventRecord *event)
  203. {
  204.     TXNObject     object = NULL;
  205.     
  206.     // Step 11 -- let MLTE grow the window.
  207.     
  208. }
  209.  
  210. // Handle a mouseClick in the zoom box of an active window.
  211. void DoZoomWindow(WindowPtr window, short part)
  212. {
  213.     TXNObject     object = NULL;
  214.     
  215.     // Step 12 -- let MLTE zoom the window.    
  216.     
  217. }
  218.  
  219. // Called when an update event is received for a window.
  220. void DoUpdate(WindowPtr window)
  221. {
  222.     GrafPtr        curPort;
  223.     TXNObject    object = NULL;
  224.     
  225.     GetPort(&curPort);
  226.     SetPort(window);
  227.     
  228.     // Step 14 -- let MLTE update the window.
  229.  
  230.     SetPort(curPort);
  231. }
  232.  
  233. //    Called when a window is activated or deactivated
  234. void DoActivate(WindowPtr window, Boolean  becomingActive)
  235. {
  236.     TXNObject    object     = NULL;
  237.     TXNFrameID     frameID    = 0;
  238.  
  239.     // Step 15 -- let MLTE Activate and Focus the TNXObject.
  240.  
  241. }
  242.  
  243. //    Called when a mouseDown occurs in the content of a window
  244. void DoContentClick(WindowPtr window, EventRecord *event)
  245. {
  246.     TXNObject    object = NULL;
  247.     
  248.     // Step 10 -- let MLTE handle the mouse down.
  249.  
  250. }
  251.  
  252. // Called for any keyDown or autoKey events, except when the Command key is held down.
  253. void DoKeyDown(EventRecord *event)
  254. {
  255.     WindowPtr    window = FrontWindow();
  256.     TXNObject    object = NULL;
  257.  
  258.     // Step 13 -- let MLTE handle the key down
  259.  
  260. }
  261.  
  262. //    Calculate sleep value for WaitNextEvent.
  263. static UInt32 GetSleep()
  264. {
  265.     UInt32        sleep;
  266.     WindowPtr    window;
  267.     TXNObject     object = NULL;
  268.     
  269.     window = FrontWindow();
  270.     
  271.     // Step 6 -- let MLTE calc the appropriate sleep time.
  272.  
  273.     sleep = GetCaretTime();                // use some other value
  274.  
  275.     return sleep;
  276. }
  277.  
  278. // Called when we get a null event.
  279. void DoIdle()
  280. {
  281.     WindowPtr    window = FrontWindow();
  282.     TXNObject    object = NULL;
  283.     
  284.     // Step 9 -- let MLTE handle idle time.
  285.  
  286. }
  287.  
  288. //    Enable and disable menus based on the current state.
  289. void AdjustMenus()
  290. {
  291.     WindowPtr    window;
  292.     TXNObject     object = NULL;
  293.     MenuHandle    menu;
  294.     Boolean        undo, redo, cutCopyClear, paste, selectAll;
  295.     
  296.     window = FrontWindow();
  297.  
  298.     menu = GetMenuHandle(mFile);
  299.     if (gNumDocuments < kMaxOpenDocuments)
  300.         EnableItem(menu, iNew);        // New is enabled when we can open more documents
  301.     else
  302.         DisableItem(menu, iNew);
  303.         
  304.     if (window != NULL)                // Printing and Close are enabled when a window is up
  305.     {
  306.         EnableItem(menu, iPageSetup);
  307.         EnableItem(menu, iPrint);    
  308.         EnableItem(menu, iClose);
  309.     }
  310.     else
  311.     {
  312.         DisableItem(menu, iPageSetup);
  313.         DisableItem(menu, iPrint);    
  314.         DisableItem(menu, iClose);
  315.     }
  316.     
  317.     menu = GetMenuHandle(mEdit);
  318.     undo = redo = cutCopyClear = paste = selectAll = false;
  319.     
  320.     if (IsDAWindow(window))
  321.         undo = redo = cutCopyClear = paste = selectAll = true;
  322.     else if (IsAppWindow(window)) 
  323.     {
  324.         // Step 16 -- decide if Undo and Redo should be enabled.
  325.         
  326.         // Step 19 -- decide if Cut, Copy, Clear should be enabled.
  327.         
  328.         // Step 23 -- decide if Paste should be enabled.
  329.         
  330.         // Step 25 -- decide if Select All should be enabled.
  331.         
  332.     }
  333.     
  334.     if (undo)
  335.         EnableItem(menu, iUndo);
  336.     else
  337.         DisableItem(menu, iUndo);
  338.  
  339.     if (redo)
  340.         EnableItem(menu, iRedo);
  341.     else
  342.         DisableItem(menu, iRedo);
  343.         
  344.     if (cutCopyClear) 
  345.     {
  346.         EnableItem(menu, iCut);
  347.         EnableItem(menu, iCopy);
  348.         EnableItem(menu, iClear);
  349.     } 
  350.     else 
  351.     {
  352.         DisableItem(menu, iCut);
  353.         DisableItem(menu, iCopy);
  354.         DisableItem(menu, iClear);
  355.     }
  356.     
  357.     if (paste)
  358.         EnableItem(menu, iPaste);
  359.     else
  360.         DisableItem(menu, iPaste);
  361.         
  362.     if (selectAll)
  363.         EnableItem(menu, iSelectAll);
  364.     else
  365.         DisableItem(menu, iSelectAll);
  366.  
  367.     InvalMenuBar();        // invalidate menubar - will update next event loop
  368. }
  369.  
  370. //    Handles items chosen from the menu bar.
  371. void DoMenuCommand(long menuResult)
  372. {
  373.     WindowPtr        window;
  374.     TXNObject        object = NULL;
  375.     short            menuID, menuItem;
  376.     short            itemHit, daRefNum;
  377.     Str255            daName;
  378.     OSStatus        status = noErr;
  379.  
  380.     window = FrontWindow();
  381.     menuID = HiWord(menuResult);    // use macros to get menu 
  382.     menuItem = LoWord(menuResult);    // item number and menu number
  383.  
  384.     switch (menuID) 
  385.     {
  386.         case mApple:
  387.             switch (menuItem) 
  388.             {
  389.                 case iAbout:        // bring up alert for About
  390.                     itemHit = Alert(rAboutAlert, NULL);
  391.                                         
  392.                 default:            // non-About items are DAs et al
  393.                     MenuHandle appleMenuHandle = GetMenuHandle(mApple);
  394.                     if (appleMenuHandle != NULL)
  395.                         GetMenuItemText(appleMenuHandle, menuItem, daName);
  396.                     daRefNum = OpenDeskAcc(daName);
  397.                     break;
  398.             }
  399.             break;
  400.         case mFile:
  401.             switch (menuItem) 
  402.             {
  403.                 case iNew:
  404.                     DoNew();
  405.                     break;
  406.                 case iClose:
  407.                     DoCloseWindow(FrontWindow());    // ignore the result
  408.                     break;
  409.                 case iPageSetup:
  410.                     // Step 34 -- let MLTE handle Page Setup.
  411.                     
  412.                     break;
  413.                 case iPrint:
  414.                     // Step 35 -- let MLTE handle Print.
  415.                     
  416.                     break;
  417.                 case iQuit:
  418.                     Terminate();
  419.                     break;
  420.             }
  421.             break;
  422.         case mEdit:            
  423.             if (IsAppWindow(window))
  424.             {
  425.                 switch (menuItem) 
  426.                 {
  427.                     case iUndo:
  428.                         // Step 17 -- let MLTE handle Undo.
  429.                         
  430.                         break;
  431.                     case iRedo:
  432.                         // Step 18 -- let MLTE handle Redo.
  433.                         
  434.                         break;
  435.                     case iCut:
  436.                         // Step 20 -- let MLTE handle Cut.
  437.                         
  438.                         break;
  439.                     case iCopy:
  440.                         // Step 21 -- let MLTE handle Copy.
  441.                         
  442.                         break;
  443.                     case iPaste:
  444.                         // Step 24 -- let MLTE handle Paste.
  445.                         
  446.                         break;
  447.                     case iClear:
  448.                         // Step 22 -- let MLTE handle Clear.
  449.                         
  450.                         break;
  451.                     case iSelectAll:
  452.                         // Step 26 -- let MLTE handle Select All.
  453.                         
  454.                         break;
  455.                     default:
  456.                         break;
  457.                 }
  458.             }    
  459.         case mFont:
  460.             DoFontMenu(menuID, menuItem, window);            
  461.             break;
  462.         case mSize:
  463.             if (IsAppWindow(window))
  464.             {
  465.                 // Step 30 -- let MLTE change selected text to selected size.
  466.                 // The structure containing attributes we would like to set.
  467.                 TXNTypeAttributes    typeAttr;
  468.                 
  469.                 // These are the possible size choices the app offers.
  470.                 static short aFontSizeList[] = {9, 10, 12, 14, 18, 24, 36};
  471.                 
  472.                 // This is the size the user chose, off 1 b/c arrays are zero-based
  473.                 short shortValue = aFontSizeList[menuItem - 1];
  474.             
  475.                 // Constant specifying size attribute -- FOUR_CHAR_CODE('size')
  476.  
  477.                 // Constant specifying the size of the size attribute -- sizeof(SInt16)
  478.  
  479.                 // move data into high byte
  480.                 
  481.  
  482.                 // Set the size attributes
  483.  
  484.             }
  485.             break;
  486.         case mStyle:
  487.             if (IsAppWindow(window))
  488.             {
  489.                 TXNTypeAttributes    typeAttr; // struct of attributes we would like to set.
  490.                 Style                newStyle;
  491.                 
  492.                 switch (menuItem) 
  493.                 {
  494.                     case iPlain:
  495.                         newStyle = normal;        // 0        
  496.                         break;
  497.                     case iBold:
  498.                         newStyle = bold;        // 1                
  499.                         break;
  500.                     case iItalic:
  501.                         newStyle = italic;        // 2                
  502.                         break;
  503.                     case iUnderline:
  504.                         newStyle = underline;    // 3                
  505.                         break;
  506.                     case iOutline:
  507.                         newStyle = outline;        // 4                
  508.                         break;
  509.                     case iShadow:
  510.                         newStyle = shadow;        // 0x10                
  511.                         break;
  512.                     case iCondensed:
  513.                         newStyle = condense;    // 0x20                
  514.                         break;                    
  515.                     case iExtended:
  516.                         newStyle = extend;        // 0x40                
  517.                         break;
  518.                     default:
  519.                         break;
  520.                 }
  521.                 // Step 31 -- let MLTE change selected text to selected style.
  522.                 //    Specify the style attributes
  523.  
  524.                 
  525.                 //    Set the style attributes
  526.  
  527.             }
  528.             break;
  529.         case mLayout:
  530.             if (menuItem <= iForceJustify)
  531.                 DoJustification(window, menuItem);
  532.             else
  533.                 DoWordWrap(window);
  534.             break;
  535.         default:
  536.             if (menuID >= kStartHierMenuID)
  537.                 DoFontMenu(menuID, menuItem, window);
  538.             break;
  539.     }
  540.     HiliteMenu(0);        // unhighlight the menu
  541.     AdjustMenus();
  542. }
  543.  
  544. // Handle font menu selections
  545. void DoFontMenu(short menuID, short menuItem, WindowPtr window)
  546. {
  547.     TXNObject object = NULL;
  548.     OSStatus  status = noErr;
  549.     
  550.     if (IsAppWindow(window))
  551.     {
  552.         // Step 29 -- let MLTE change selected text to selected font.
  553.  
  554.         if (status != noErr)
  555.             AlertUser(eNoFontName);
  556.     }
  557. }
  558.  
  559. // Set the justification tag
  560. void DoJustification(WindowPtr window, short menuItem)
  561. {
  562.     TXNObject        object = NULL;
  563.     MenuHandle         layoutMenu;
  564.     OSStatus        status = noErr;
  565.     
  566.     layoutMenu = GetMenuHandle(mLayout);
  567.     
  568.     if (IsAppWindow(window)) 
  569.     {
  570.         SInt32            justification;
  571.         TXNControlTag    controlTag[1];
  572.         TXNControlData    controlData[1];
  573.         
  574.         for (int i = 1; i <= iForceJustify; i++)    // brute technique to uncheck last item
  575.             CheckItem(layoutMenu, i, false);
  576.         
  577.         switch (menuItem) 
  578.         {
  579.             case iDefaultJustify:
  580.                 justification = kTXNFlushDefault;    // flush according to line direction                        
  581.                 break;
  582.             case iLeftJustify:
  583.                 justification = kTXNFlushLeft;
  584.                 break;
  585.             case iRightJustify:
  586.                 justification = kTXNFlushRight;
  587.                 break;
  588.             case iCenterJustify:
  589.                 justification = kTXNCenter;
  590.                 break;
  591.             case iFullJustify:
  592.                 justification = kTXNFullJust;
  593.                 break;
  594.             case iForceJustify:
  595.                 justification = kTXNForceFullJust;    // flush left for all scripts
  596.                 break;
  597.             default:
  598.                 break;
  599.         }
  600.         CheckItem(layoutMenu, menuItem, true);        // check this menu item
  601.         
  602.         // Step 33 -- let MLTE et the document's text justification.
  603.  
  604.     }
  605. }
  606.  
  607. // Toggle word wrap
  608. void DoWordWrap(WindowPtr window)
  609. {
  610.     TXNObject    object = NULL;
  611.     MenuHandle     layoutMenu;
  612.     OSStatus    status = noErr;
  613.     
  614.     layoutMenu = GetMenuHandle(mLayout);
  615.     
  616.     if (IsAppWindow(window))
  617.     {
  618.         TXNControlTag    controlTag[1];
  619.         TXNControlData    controlData[1];
  620.  
  621.         // Step 32 -- let MLTE set the document's word wrap state.
  622.         
  623.     }
  624. }
  625.  
  626. // Create a new document and window.
  627. void DoNew(void)
  628. {
  629.     WindowPtr        window;
  630.     MenuHandle         layoutMenu;
  631.     OSStatus        status = noErr;
  632.     
  633.     window = GetNewCWindow(rDocWindow, NULL, (WindowPtr)-1L);
  634.     
  635.     if (window != NULL) 
  636.     {
  637.         // Step 3 -- create a new TXNObject.
  638.         
  639.         if (status == noErr)
  640.         {        
  641.             // Step 4 -- store a reference to the oject and the frameID
  642.  
  643.             AdjustMenus();
  644.             gNumDocuments++;            // will be decremented in DoCloseWindow
  645.             DoJustification(window, iDefaultJustify);     // set justification default for new doc
  646.             layoutMenu = GetMenuHandle(mLayout);
  647.             CheckItem(layoutMenu, iAutoWrap, true);        // check this menu item
  648.             ShowWindow(window);
  649.         }
  650.         else
  651.         {
  652.             CloseWindow(window);
  653.             window = NULL;
  654.             AlertUser(eNoWindow);    // TXNNewObject failed -  tell user
  655.         }
  656.     }
  657.     else
  658.         AlertUser(eNoWindow);        // window was null - tell user
  659. }
  660.  
  661. // Close a window. Handles desk accessory and application windows.
  662. Boolean DoCloseWindow(WindowPtr window)
  663. {
  664.     TXNObject    object = NULL;
  665.     Boolean     isClosed = true;
  666.  
  667.     if (IsDAWindow(window))
  668.     {
  669.         CloseDeskAcc(GetWindowKind(window));
  670.     }
  671.     else if (IsAppWindow(window))
  672.     {
  673.  
  674.         if (isClosed) 
  675.         {
  676.             // Step 5 -- delete the TXNObject.
  677.             
  678.             CloseWindow(window);
  679.             DisposePtr((Ptr) window);
  680.             gNumDocuments -= 1;
  681.         }
  682.     }
  683.     AdjustMenus();
  684.     return isClosed;
  685. }
  686.  
  687. // Clean up and exit. Close all of the windows.
  688. void Terminate()
  689. {
  690.     WindowPtr    aWindow;
  691.     Boolean        closed;
  692.     OSStatus    status = noErr;
  693.     
  694.     closed = true;
  695.     do {
  696.         aWindow = FrontWindow();                // get current front window
  697.         if (aWindow != NULL)
  698.             closed = DoCloseWindow(aWindow);    // close this window
  699.     }
  700.     while (closed && (aWindow != NULL));
  701.  
  702.     if (closed)
  703.     {
  704.         // Step 28 -- dispose font menu object before terminating Textension.
  705.         
  706.         
  707.         // Step 2 -- terminate MLTE.
  708.         
  709.         
  710.         ExitToShell();
  711.     }
  712. }
  713.  
  714. // Check whether a window is a document window created by the application.
  715. // These windows are distinguished from desk accessories, dialogs, and  
  716. // other windows by their windowKind userKind.
  717. Boolean IsAppWindow(WindowPtr window)
  718. {
  719.     OSErr status = noErr;
  720.     
  721.     return (window != NULL) && (((WindowPeek) window)->windowKind == userKind);
  722. }
  723.  
  724. // Check to see if a window belongs to a desk accessory.
  725. Boolean IsDAWindow(WindowPtr window)
  726. {
  727.     if (window == NULL)
  728.         return false;
  729.     else    // DA windows have negative windowKinds
  730.         return ((WindowPeek) window)->windowKind < 0;
  731. }
  732.  
  733. //    Display an alert that tells the user an error occurred.
  734. void AlertUser(short error)
  735. {
  736.     short        itemHit;
  737.     Str255        message;
  738.  
  739.     SetCursor(&qd.arrow);
  740.     GetIndString(message, kErrStrings, error);
  741.     ParamText(message, (ConstStr255Param)"",(ConstStr255Param)"", (ConstStr255Param)"");
  742.     itemHit = Alert(rUserAlert, NULL);
  743. }
  744.  
  745. // Used whenever a fatal error happens
  746. void BigBadError(short error)
  747. {
  748.     AlertUser(error);
  749.     ExitToShell();
  750. }
  751.  
  752. // install the core apple event handlers
  753. void InstallAppleEventHandlers(void)
  754. {
  755.     long result;
  756.     
  757.     OSErr err = Gestalt(gestaltAppleEventsAttr, &result);
  758.     
  759.     if (err == noErr)
  760.     {    // we should check the AEInstallEventHandler return value but since it's just a sample...
  761.         AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
  762.         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(HandleOdoc), 0, false);
  763.         AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(HandlePdoc), 0, false);
  764.         AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
  765.     }
  766. }
  767.  
  768. // Respond to an open application apple event
  769. pascal OSErr HandleOapp (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
  770. {
  771. #pragma unused (aevt, reply, refCon)
  772.     return noErr;
  773. }
  774.  
  775. // Respond to an open document apple event
  776. pascal OSErr HandleOdoc (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
  777. {
  778. #pragma unused (aevt, reply, refCon)
  779.     return errAEEventNotHandled;
  780. }
  781.  
  782. // Respond to a print apple event
  783. pascal OSErr HandlePdoc (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
  784. {
  785. #pragma unused (aevt, reply, refCon)
  786.     return errAEEventNotHandled;
  787. }
  788.  
  789. // Respond to a quit apple event
  790. pascal OSErr HandleQuit (const AppleEvent *aevt, AEDescList *reply, UInt32 refCon)
  791. {
  792. #pragma unused (aevt, reply, refCon)
  793.     Terminate();
  794.     return noErr;
  795. }